// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
// import websdk from 'easemob-websdk'
import App from './App'
import router from './router'
import store from './store'
import axios from 'axios'
// import vueWechatTitle from 'vue-wechat-title'


import './assets/css/common.css'
import './assets/js/jquery-2.2.4.min.js'
import './assets/js/flexible.js'
import { ConfirmPlugin, DatetimePlugin, AlertPlugin, ToastPlugin } from 'vux'

// Vue.use(DatetimePlugin)
Vue.use(ConfirmPlugin)
Vue.use(AlertPlugin)
Vue.use(ToastPlugin)
import InfiniteScroll from 'vue-infinite-scroll'
Vue.use(InfiniteScroll)

// Vue.use(vueWechatTitle) //微信头部标题
    // 移除移动端点击延迟
const FastClick = require('fastclick');
FastClick.attach(document.body);

Vue.config.productionTip = false
Vue.config.devTool = true
    /* eslint-disable no-new */
Vue.filter('price', function(value) {
    return value.toFixed(2)
})

import websdk from 'easemob-websdk'
let webIM = window.WebIM = websdk
Vue.prototype.$WebIM = webIM
const imConn = new webIM.connection({  //创建连接
    isMultiLoginSessions: webIM.config.isMultiLoginSessions,
    https: typeof webIM.config.https === 'boolean' ? webIM.config.https : location.protocol === 'https:',
    url: webIM.config.xmppURL,
    isAutoLogin: true,
    heartBeatWait: webIM.config.heartBeatWait,
    autoReconnectNumMax: webIM.config.autoReconnectNumMax,
    autoReconnectInterval: webIM.config.autoReconnectInterval,
    apiUrl: webIM.config.apiURL
})

// imConn.listen({
//     onOnline: function() {  
//         console.log('本机网络连接成功')
//     }, 
//     onOpened: function (message) { //连接成功
//         console.log('连接打开=>', message)
//     },
//     onClosed: function (message) { //连接关闭
//         console.log('连接关闭=>', message)
//     }
// })
const options = {
  apiUrl: WebIM.config.apiURL,
  user: '15594818160',
  pwd: '123456',
  appKey: WebIM.config.appkey
}
Vue.prototype.$imConn = imConn
Vue.prototype.$imoption = options


Vue.prototype.$axios = axios;

new Vue({
    el: '#app',
    router,
    components: { App },
    template: '<App/>',
    store
})


Vue.prototype.uploadImg = function(files, imgsrc) {
    var file = files[0];
    var size = file.size;

    const isJPG = (file.type === 'image/jpeg') || (file.type === 'image/png');
    const isLt3M = file.size / 1024 / 1024 < 8;
    if (!isJPG) {
        this.$vux.toast.text('上传头像图片只能是 JPG/png 格式!');
        return;
    }
    if (!isLt3M) {
        this.$vux.toast.text('上传图片不能大于8M', 'middle');
        return;
    }
    var formData = new FormData();
    var name = file.name;
    formData.append("file", file);
    formData.append("name", name);
    // let config = {  设置请求头
    //   headers: {
    //     'Content-Type': 'multipart/form-data'
    //   }
    // }
    return this.$axios({
            method: 'POST',
            url: 'http://www.chenyekeji.com/21market' + "/image/upload2",
            // url: 'http://172.17.1.157:8080' + "/image/upload2",
            // crossDomain: true,
            //traditional :true,//传递数组
            data: formData,
            dataType: "text",
            // 告诉jQuery不要去处理发送的数据
            processData: false,
            // 告诉jQuery不要去设置Content-Type请求头
            contentType: false,
            // responseType: 'json',
        })
        // .then((res) => {
        //     console.log(res)
        //     // imgsrc = res.data
        //     return res
        // })
        // .catch((error) => {
        // that.isLoadingShow = false;
        // setTimeout(() => {
        //     that.isDialogShow = true;
        // },100);
        // that.dialogTit = '服务器错误';
        // console.log('错误了'+ error);
        // });
}

Date.prototype.format = function(format) {
    var date = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),
        "S+": this.getMilliseconds()
    };
    if (/(y+)/i.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    for (var k in date) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
        }
    }
    return format;
}
Vue.prototype.numbers = function addNum(num1,num2){
    var sq1,sq2,m;
    try{sq1=num1.toString().split(".")[1].length;} catch(e){sq1=0;}
    try{sq2=num2.toString().split(".")[1].length;} catch(e){sq2=0;}
    
    m=Math.pow(10,Math.max(sq1,sq2));
    return ( num1 * m + num2 * m ) / m;
    }

router.beforeEach((to, from, next) => {
  window.document.title = to.meta.title;
  next()
})